import asyncio
import re
import time
from itertools import zip_longest
import tkinter
from pyppeteer import launch


async def main():
    # 启动浏览器
    browser = await launch(executablePath='C:\Program Files\Google\Chrome\Application\chrome.exe',headless=False)
    # 新建一个页面
    page = await browser.newPage()
    try:
        import ctypes
        user32 = ctypes.windll.user32
        screen_width = user32.GetSystemMetrics(0)
        screen_height = user32.GetSystemMetrics(1)
    except ImportError:
        screen_width = 1920
        screen_height = 1080

        # 设置视口大小以模拟最大化窗口
    await page.setViewport({'width': screen_width, 'height': screen_height})
    # 访问指定网址
    await page.goto('http://www.meps-k.com/search/all?spuName',timeout=600000000000)
    # 将浏览器界面最大化
    # await maximize_viewport(page)
    # 等待一段时间以确保页面渲染完成
    # await asyncio.sleep(1)
    # 获取页面数据
    content = await page.content()
    # print(content)
    #re获取商品名称
    pattern_name = r'h-\[34px\]">(.*?)</div>'
    goods_name=re.findall(pattern_name,content)
    # print(goods_name)
    # 获取商品销售价
    pattern_sell_price=r'class="text-\[#3C3C3C\] font-bold text-\[14px\]">(.*?)</span>'
    goods_price_list=re.findall(pattern_sell_price,content,flags=re.S)
    # print(goods_price_list)
    sell_of_goods=[s.replace('\n','') for s in goods_price_list]
    sell_of_goods=[s.replace(' ','') for s in sell_of_goods]
    # print(sell_of_goods)
    # 获取商品原价
    pattern_cost_price=r'class="text-\[#85878C\] ml-10px text-\[12px\]">(.*?)</del>'
    goods_price_list = re.findall(pattern_cost_price, content, flags=re.S)
    # print(goods_price_list)
    cost_of_goods = [s.replace('\n', '') for s in goods_price_list]
    cost_of_goods = [s.replace(' ', '') for s in cost_of_goods]
    # 点击分页
    element = await  page.xpath('//*[@id="__layout"]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/div/div/div[2]/ul/li[2]')
    await  element[0].click()
    await asyncio.sleep(1)
    # 获取页面数据
    content = await page.content()
    # 获取分页商品名称
    pattern_name = r'h-\[34px\]">(.*?)</div>'
    goods_name1 = re.findall(pattern_name, content)
    # 获取分页商品销售价
    pattern_sell_price = r'class="text-\[#3C3C3C\] font-bold text-\[14px\]">(.*?)</span>'
    goods_price_list1 = re.findall(pattern_sell_price, content, flags=re.S)
    sell_of_goods1 = [s.replace('\n', '') for s in goods_price_list1]
    sell_of_goods1 = [s.replace(' ', '') for s in sell_of_goods1]
    # 获取分页商品原价
    pattern_cost_price = r'class="text-\[#85878C\] ml-10px text-\[12px\]">(.*?)</del>'
    goods_price_list1 = re.findall(pattern_cost_price, content, flags=re.S)
    cost_of_goods1 = [s.replace('\n', '') for s in goods_price_list1]
    cost_of_goods1 = [s.replace(' ', '') for s in cost_of_goods1]
    # cost_of_goods=[s for s in cost_of_goods if s]  #当有些商品销售价等于原价时，原价为'',去除''。
    # # 点击分页
    # element = await  page.xpath('//*[@id="__layout"]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/div/div/div[2]/ul/li[3]')
    # await  element[0].click()
    # await asyncio.sleep(1)
    # # 获取页面数据
    # content = await page.content()
    # # 获取分页商品名称
    # pattern_name = r'h-\[34px\]">(.*?)</div>'
    # goods_name2 = re.findall(pattern_name, content)
    # # 获取分页商品销售价
    # pattern_sell_price = r'class="text-\[#3C3C3C\] font-bold text-\[14px\]">(.*?)</span>'
    # goods_price_list2 = re.findall(pattern_sell_price, content, flags=re.S)
    # sell_of_goods2 = [s.replace('\n', '') for s in goods_price_list2]
    # sell_of_goods2 = [s.replace(' ', '') for s in sell_of_goods2]
    # # 获取分页商品原价
    # pattern_cost_price = r'class="text-\[#85878C\] ml-10px text-\[12px\]">(.*?)</del>'
    # goods_price_list2 = re.findall(pattern_cost_price, content, flags=re.S)
    # cost_of_goods2 = [s.replace('\n', '') for s in goods_price_list2]
    # cost_of_goods2 = [s.replace(' ', '') for s in cost_of_goods2]
    # 所有商品名称
    goods_name.extend(goods_name1)
    # goods_name.extend(goods_name2)

    # 所有商品销售价
    sell_of_goods.extend(sell_of_goods1)
    # sell_of_goods.extend(sell_of_goods2)

    # 所有商品原价
    cost_of_goods.extend(cost_of_goods1)
    # cost_of_goods.extend(cost_of_goods2)
    # 关闭浏览器
    await browser.close()
    return [goods_name,sell_of_goods,cost_of_goods]
# 运行异步函数
resp=asyncio.get_event_loop().run_until_complete(main())
# 获取爬取后的商品信息
print(resp)
for goods_name , sell_of_goods,cost_of_goods  in zip_longest (resp[0],resp[1],resp[2]):
    if not cost_of_goods:
        cost_of_goods=sell_of_goods
    print('商品名称：{}，商品销售价：{}，商品成本价：{}'.format(goods_name,sell_of_goods,cost_of_goods))